package test01.yan.t5;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class AddLineNumber {
    public static void main(String[] args) {
        // 检查命令行参数以确保提供了输入和输出文件名
        if (args.length != 2) {
            System.out.println("Usage: java AddLineNumber <inputFile> <outputFile>");
            return;
        }

        String inputFile = args[0];
        String outputFile = args[1];

        try (
                // 创建BufferedReader来读取输入文件
                BufferedReader reader = new BufferedReader(new FileReader(inputFile));
                // 创建BufferedWriter来写入输出文件
                BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))
        ) {
            String line;
            int lineNumber = 1;

            // 按行读取输入文件
            while ((line = reader.readLine()) != null) {
                // 在每行前添加行号，并写入输出文件
                writer.write(lineNumber + ": " + line);
                writer.newLine(); // 添加换行符以开始新的一行
                lineNumber++;
            }
        } catch (IOException e) {
            System.err.println("Error reading or writing files: " + e.getMessage());
        }
    }
}
